package com.zhn.resourcej.controller;


import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhn.resourcej.entity.Teachers;
import com.zhn.resourcej.service.ITeachersService;
import com.zhn.resourcej.web.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.io.IOException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Mr.Seaning
 * @since 2023-12-04
 */
@RestController
@RequestMapping("/teachers")
@RequiredArgsConstructor
@Api(tags = "教师")
public class TeachersController {
    private final ITeachersService teachersService;

    /*excel批量导入,excel格式固定*/
    @PostMapping("/import")
    @ApiOperation("批量导入")
    public Result imports(MultipartFile file) throws IOException {
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
        List<Teachers> all = reader.readAll(Teachers.class);
        teachersService.saveBatch(all);
        return Result.success("导入成功");
    }

    @GetMapping("/list")
    @ApiOperation("查询教师列表")
    public Result list(int page, int limit) {
        IPage<Teachers> p = teachersService.page(new Page<Teachers>(page, limit));
        return Result.success("查询成功", p.getRecords(), p.getTotal());
    }
    @PostMapping("/add")
    @ApiOperation("添加教师")
    public Result add(@RequestBody Teachers teachers) {
        teachersService.save(teachers);
        return Result.success("添加成功");
    }
    @PostMapping("/update")
    @ApiOperation("修改教师")
    public Result update(@RequestBody Teachers teachers) {
        teachersService.updateById(teachers);
        return Result.success("修改成功");
    }
    @PostMapping("/delete/{id}")
    @ApiOperation("删除教师")
    public Result delete(@PathVariable Integer id) {
        teachersService.removeById(id);
        return Result.success("删除成功");
    }
}
